package com.example.reference_book.controller;

import com.example.reference_book.entity.Attention;
import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.Collect;
import com.example.reference_book.service.CollectService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收藏表(Collect)表控制层
 *
 * @author makejava
 * @since 2022-06-07 22:19:03
 */
@RestController
@RequestMapping("collect")
public class CollectController{
    /**
     * 服务对象
     */
    @Resource
    private CollectService collectService;

    @GetMapping("/allCollectData/{id}")
    public ResponseEntity<List<Book>> queryCollect(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.collectService.queryCollect(id));
    }

    /**
     * 分页查询
     *
     * @param collect 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Collect>> queryByPage(Collect collect, PageRequest pageRequest) {
        return ResponseEntity.ok(this.collectService.queryByPage(collect, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Collect> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.collectService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param collect 实体
     * @return 新增结果
     */
    @PostMapping("/addCollect")
    public ResponseEntity<Collect> add(Collect collect) {
        return ResponseEntity.ok(this.collectService.insert(collect));
    }

    /**
     * 编辑数据
     *
     * @param collect 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Collect> edit(Collect collect) {
        return ResponseEntity.ok(this.collectService.update(collect));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.collectService.deleteById(id));
    }

    @PostMapping("/deleteCollect")
    public ResponseEntity<Boolean> deleteCollect(Collect collect) {
        return ResponseEntity.ok(this.collectService.deleteCollect(collect));
    }

    @GetMapping("/searchCollectData/{id}/{bookId}")
    public ResponseEntity<Boolean> isCollect(@PathVariable("id") Integer id,@PathVariable("bookId") Integer bookId) {
        List<Book> list=this.collectService.isCollect(id,bookId);
        if(list.isEmpty()){
            return ResponseEntity.ok(false);
        }
        return ResponseEntity.ok(true);
    }
}

